package com.gl.goods.ui.main.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.gl.goods.R;
import com.gl.goods.base.AppManager;
import com.gl.goods.base.BaseActivity;
import com.gl.goods.base.MyApplication;
import com.gl.goods.ui.main.fragment.AdminFragment;
import com.gl.goods.ui.main.fragment.HomeFragment;
import com.gl.goods.ui.main.fragment.ShopFragment;
import com.gl.goods.ui.main.fragment.UserFragment;
import com.gl.goods.ui.splash.FileUtil;
import com.gl.library.utils.StatusBarUI;
import com.hjq.toast.ToastUtils;
import com.youth.banner.Banner;

import org.simple.eventbus.EventBus;

import java.io.IOException;

import butterknife.BindView;

public class MainActivity extends BaseActivity {
    /**
     * btn集合
     */
    private Button[] mTabs;
    /**
     * fragments集合
     */
    private Fragment[] fragments;
    /**
     * 当前位置和选中位置
     */
    private int index, currentTabIndex = 0;
    /**管理员*/
    @BindView(R.id.btn_admin)
    Button mAdminBtn;
    /**商家*/
    @BindView(R.id.btn_shop)
    Button mShopBtn;

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        StatusBarUI.setStatusBarTranslucent(mActivitySelf);
        setTitleVisibility(true);
        /** 基础设置*/
        setupView();
        if (MyApplication.getInstance().isLogin()){
            if (memberId == 16){
                mAdminBtn.setVisibility(View.VISIBLE);
            }else if (memberId == 17){
                mShopBtn.setVisibility(View.VISIBLE);
            }
        }
    }
    public void onTabClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_main_home:
                showHomeFragment();
                index = 0;
                StatusBarUI.setStatusBarTranslucent(getActivity());
                break;
            case R.id.btn_admin:
                StatusBarUI.setStatusBarTranslucent(getActivity());
                if (MyApplication.getInstance().isLogin()){
                    showAdminFragment();
                    index = 1;
                    mTabs[index].setTextColor(getResources().getColor(R.color.FD1D1D));
                }else {
                    enterLoginActivity();
                }
                break;
            case R.id.btn_shop:
                StatusBarUI.setStatusBarTranslucent(getActivity());
                if (MyApplication.getInstance().isLogin()){
                    showShopFragment();
                    index = 2;
                    mTabs[index].setTextColor(getResources().getColor(R.color.FD1D1D));
                }else {
                    enterLoginActivity();
                }
                break;
            case R.id.btn_main_me:
                StatusBarUI.setStatusBarTranslucent(getActivity());
                if (MyApplication.getInstance().isLogin()){
                    showUserFragment();
                    index = 3;
                    mTabs[index].setTextColor(getResources().getColor(R.color.FD1D1D));
                }else {
                    enterLoginActivity();
                }
                break;
        }
        mTabs[currentTabIndex].setSelected(false);

        /**
         * 把当前tab设为选中状态
         */
        mTabs[index].setSelected(true);
        setTextColor(index);
        currentTabIndex = index;
    }
    /**
     * 设置文字颜色
     */
    private void setTextColor(int index) {
        mTabs[0].setTextColor(getResources().getColor(R.color.color_333333));
        mTabs[1].setTextColor(getResources().getColor(R.color.color_333333));
        mTabs[2].setTextColor(getResources().getColor(R.color.color_333333));
        mTabs[3].setTextColor(getResources().getColor(R.color.color_333333));
        mTabs[index].setTextColor(getResources().getColor(R.color.color_333333));
    }

    /**
     * 基础设置
     */
    private void setupView() {
        mTabs = new Button[4];
        mTabs[0] = findViewById(R.id.btn_main_home);
        mTabs[1] = findViewById(R.id.btn_admin);
        mTabs[2] = findViewById(R.id.btn_shop);
        mTabs[3] = findViewById(R.id.btn_main_me);
        /**
         * 把第一个tab设为选中状态
         */
        mTabs[0].setSelected(true);
        mTabs[0].setTextColor(getResources().getColor(R.color.color_333333));
        fragments = new Fragment[4];
        showHomeFragment();
//        StatusBarUI.setStatusBarColor(getActivity(),Color.parseColor("#FC6900"));
    }
    /**
     * 首页Fragment
     */
    public void showHomeFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideAllFragment(fragmentTransaction);
        if (fragments[0] == null) {
            fragments[0] = new HomeFragment();
            fragmentTransaction.add(R.id.main_layout, fragments[0]);
        }
        commitShowFragment(fragmentTransaction, fragments[0]);
    }

    /**
     * AdminFragment
     */
    public void showAdminFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideAllFragment(fragmentTransaction);
        if (fragments[1] == null) {
            fragments[1] = new AdminFragment();
            fragmentTransaction.add(R.id.main_layout, fragments[1]);
        }
        commitShowFragment(fragmentTransaction, fragments[1]);
        EventBus.getDefault().post("点击切换admin", "切换admin");
    }

    /**
     * ShopFragment
     */
    public void showShopFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideAllFragment(fragmentTransaction);
        if (fragments[2] == null) {
            fragments[2] = new ShopFragment();
            fragmentTransaction.add(R.id.main_layout, fragments[2]);
        }
        commitShowFragment(fragmentTransaction, fragments[2]);
        EventBus.getDefault().post("点击切换shop", "切换shop");
    }

    /**
     * 我的Fragment
     */
    private void showUserFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideAllFragment(fragmentTransaction);
        if (fragments[3] == null) {
            fragments[3] = new UserFragment();
            fragmentTransaction.add(R.id.main_layout, fragments[3]);
        }
        commitShowFragment(fragmentTransaction, fragments[3]);
        EventBus.getDefault().post("点击切换我的", "切换我的");
    }

    public void commitShowFragment(FragmentTransaction fragmentTransaction, Fragment fragment) {
        fragmentTransaction.show(fragment);
        fragmentTransaction.commit();
    }
    /**
     * 隐藏Fragment
     */
    public void hideAllFragment(FragmentTransaction fragmentTransaction) {
        hideFragment(fragmentTransaction, fragments[0]);
        hideFragment(fragmentTransaction, fragments[1]);
        hideFragment(fragmentTransaction, fragments[2]);
        hideFragment(fragmentTransaction, fragments[3]);
    }

    private void hideFragment(FragmentTransaction fragmentTransaction, Fragment fragment) {
        if (fragment != null) {
            fragmentTransaction.hide(fragment);
        }
    }

    @Override
    public void widgetClick(View view) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        StatusBarUI.setStatusBarTranslucent(mActivitySelf);
    }

    /**
     * 再按一次退出
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (doubleClickExit()){
                AppManager.getAppManager().finishAllActivity();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    private static long mExitTime;
    public static boolean doubleClickExit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            ToastUtils.show("再按一次退出");
            mExitTime = System.currentTimeMillis();
            return false;
        }
        return true;
    }
}
